<?php

  require_once 'util.php';
  require_once 'add_tag.php';
  require_once 'check_login_status.php';
  require_once 'contact_list.php';
  require_once 'db_connection.php'; 
  require_once 'delete_tag.php';
  require_once 'get_comments.php';
  require_once 'get_page_rating.php';
  require_once 'get_page_rating_bulk.php'; 
  require_once 'get_page_tags.php';
  require_once 'get_pages_with_tag.php';
  require_once 'invite_user.php';
  require_once 'link.php';
  require_once 'new_comment.php';
  require_once 'rate_comment.php';
  require_once 'rate_tag.php';
  require_once 'register_new_user.php';
  require_once 'register_page.php';
  require_once 'update_page_rating.php';
  require_once 'update_title.php';
  require_once 'user_login.php';
  require_once 'user_logout.php';
  require_once 'ip_to_country.php'; 

  session_start();
  if ($_REQUEST['command'] == "verify_user") {
    header("Content-type: text/html");
  } else {
    header("Content-type: text/xml");
  }
  $connection = connectDb();
	$response = "";
  logUserAction();   
   
	switch($_REQUEST['command']) {
    case "check_login_status":
      $response = checkLoginStatus();
      break;
    case "user_login":
      $response = userLogin($_REQUEST['user_name'], $_REQUEST['password']);
      break;
    case "user_logout":
      $response = userLogout();
      break;
  	case "get_comments":
	    $response = getComments($_REQUEST['url']);
      break;
    case "new_comment":
	   $response = newComment($_REQUEST['url'], $_REQUEST['content'],
                            $_REQUEST['parent_id']);
      break;
    case "rate_comment":
      $response = rateComment($_REQUEST['comment_id'], $_REQUEST['up']);
      break;
    case "register_new_user":
      $response = registerNewUser($_REQUEST['user'], $_REQUEST['password'],
                                  $_REQUEST['email']);
      break;
    case "update_page_rating":
      $response = updatePageRating($_REQUEST['url'], $_REQUEST['url_parameter'],
                                   $_REQUEST['username'], $_REQUEST['title'],
                                   $_REQUEST['rating']);
      break;
    case "get_page_rating":
      $response = getPageRating($_REQUEST['url'], $_REQUEST['url_parameter']);
      break; 
    case "get_page_rating_bulk":
      $response = getPageRatingBulk($_REQUEST['url_string'], $_REQUEST['params_string'],
                                    $_REQUEST['username']);
      break; 
    case "add_tag":
      $response = addTag($_REQUEST['url'], $_REQUEST['url_parameter'],
                         $_REQUEST['tag']);
      break;
    case "get_page_tags":
      $response = getPageTags($_REQUEST['url'], $_REQUEST['url_parameter']);
      break;
    case "get_recommended_page_tags":
      $response = getRecommendedPageTags($_POST['url'], $_POST['url_parameter']);
      break;
    case "rate_tag":
      $response = rateTag($_REQUEST['url'], $_REQUEST['url_parameter'],
                          $_REQUEST['tag'], $_REQUEST['up']);
      break;
    case "delete_tag":
      $response = deleteTag($_REQUEST['url'], $_REQUEST['url_parameter'],
                            $_REQUEST['tag']);
      break;
    case "get_pages_with_tag":
      $response = getPagesWithTag($_REQUEST['tag']);
      break;
    case "follow_contact":
      $response = followContact($_REQUEST['username']);
      break;
    case "unfollow_contact":
      $response = unfollowContact($_REQUEST['username']);
      break;
    case "get_contact_list":
      $response = getContactList();
      break;
    case "add_link":
      $response = addLink($_REQUEST['from_url'], $_REQUEST['from_url_param'],
                          $_REQUEST['to_url'], $_REQUEST['to_url_param'], $_REQUEST['note']);
      break;
    case "delete_link":
      $response = deleteLink($_REQUEST['from_url'], $_REQUEST['from_url_param'],
                          $_REQUEST['to_url'], $_REQUEST['to_url_param']);
      break;
    case "get_links":
      $response = getLinks($_REQUEST['url'], $_REQUEST['url_param']);
      break;
    case "rate_link":
      $response = rateLink($_REQUEST['from_url'], $_REQUEST['from_url_param'], 
                           $_REQUEST['to_url'], $_REQUEST['to_url_param'], $_REQUEST['up']);
      break;
    case "get_link_comment":
      $response = getLinkComment($_REQUEST['from_url'], $_REQUEST['from_url_param'],
                                 $_REQUEST['to_url'], $_REQUEST['to_url_param']);
      break;
    case "verify_user":
      $response = verifyUser($_REQUEST['confirm_code']);
      break;
    case "invite_user":
      $response = inviteUser($_REQUEST['email']);
      break;
    case "get_title":
      $response = getTitle($_REQUEST['url'], $_REQUEST['url_param']);
      break;
    case "update_title":
      $response = updateTitle($_REQUEST['url'], $_REQUEST['url_param'],
           $_REQUEST['title']);
      break;
	}
	
	mysql_close($connection);

  if ($_REQUEST['command'] == "verify_user") {
    echo "<html>";
    echo "<head> <title> User verification </title></head>";
    echo "<body>";
    echo $response;
    echo "</body>";
    echo "</html>";
  } else {
    echo "<xmlresponse>";
    echo $response ;
    echo "</xmlresponse>";
  }

?>
